//
//  FollowViewController.m
//  XPhoto
//
//  Created by baboy on 2019/2/20.
//  Copyright © 2019 baboy. All rights reserved.
//

#import "EventViewController.h"
#import "Post.h"
#import "PhotoMapCell.h"
#import "Group.h"
#import "EventCell.h"
#import "BKIndicator.h"
#import "Theme.h"
#import "UserProfileViewController.h"
#import "BKNavigationController.h"


@interface EventViewController ()<EventCellDelegate>
@property(nonatomic, strong) NSMutableArray<Group*> *groups;
@property(nonatomic, strong) NSMutableArray<Event*> *events;
@end

@implementation EventViewController

- (id)init{
    if (self = [super initWithNibName:NSStringFromClass([self class]) bundle:nil]){
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.tableView.tableFooterView = [UIView new];
    [self searchWithMaxId:0];
}
- (void)searchWithMaxId:(NSInteger)maxId{
    EventSearchArgs *args = [[EventSearchArgs alloc] init];
    args.maxId = maxId;
    [Event searchWithArgs:args
                 callback:^(id  _Nonnull task, id  _Nonnull events, NSError * _Nonnull error) {
                     if (error) {
                         [BKIndicator showMessageAndFadeOut:error.localizedDescription completion:nil];
                         return;
                     }
                     if(!maxId){
                         self.events = [NSMutableArray array];
                         self.groups = [NSMutableArray array];
                     }
                     [self groupEvents:events];
                 }];
}
- (void)groupEvents:(NSArray*)events{
    if (!events || events.count == 0) {
        [self.tableView reloadData];
        return;
    }
    for (NSInteger i = 0, n = events.count; i<n; i++) {
        Event *evt = [events objectAtIndex:i];
        NSTimeInterval t = [[NSDate date] timeIntervalSinceDate:evt.createTime];
        NSString *grpName = NSLocalizedString(@"early", @"标签");
        if (t <= 7*24*3600) {
            grpName = NSLocalizedString(@"in_week", @"标签");
        }else if (t <= 30*24*3600) {
            grpName = NSLocalizedString(@"in_month", @"标签");
        }
        Group * grp = nil;
        for(Group * g in self.groups){
            if ([g.title isEqualToString:grpName]) {
                grp = g;
                break;
            }
        }
        if (!grp) {
            grp = [Group new];
            grp.title = grpName;
            grp.data = [NSMutableArray array];
            [self.groups addObject:grp];
        }
        [grp.data addObject:evt];
    }
    [self.events addObjectsFromArray:events];
    [self.tableView reloadData];
}
- (void)loadMore:(BKTableView *)tableView{
    NSInteger maxId = 0;
    if (self.events.count>0) {
        maxId = self.events.lastObject.id;
    }
    [self searchWithMaxId:maxId];
}
#pragma UITableView Delegate

- (EventCell *)eventCell{
    static NSString *CELL_ID = @"EventCell";
    EventCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CELL_ID];
    if (!cell) {
        cell = loadViewFromNib([EventCell class], self);
        cell.delegate = self;
    }
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    NSInteger n = self.groups.count;
    return n;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    Group *grp = [self.groups objectAtIndex:section];
    NSInteger n = grp.data.count;
    return n;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    CGFloat h = 30;
    return h;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    Group *grp = [self.groups objectAtIndex:indexPath.section];
    
    Event *event = [grp.data objectAtIndex:indexPath.row];
    EventCell *eventCell = [self eventCell];
    CGFloat h = [eventCell heightForEvent:event width:self.view.bounds.size.width];
    return h;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    Group *grp = [self.groups objectAtIndex:section];
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, 30)];
    CGRect labelFrame = headerView.bounds;
    labelFrame.origin.x += 10;
    labelFrame.size.width -= 10;
    UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
    label.text = grp.title;
    label.font = THEME_TABLE_CELL_TITLE_FONT;
    label.textColor = THEME_TABLE_CELL_TITLE_COLOR;
    [headerView addSubview:label];
    return headerView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    Group *grp = [self.groups objectAtIndex:indexPath.section];
    Event *event = [grp.data objectAtIndex:indexPath.row];
    
    EventCell *eventCell = [self eventCell];
    eventCell.event = event;
    
    return eventCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    Group *grp = [self.groups objectAtIndex:indexPath.section];
    Event *evt = [grp.data objectAtIndex:indexPath.row];
    DLOG(@"event:%@",evt);
}

- (void)openUserProfileViewController:(NSInteger)uid{
    UserProfileViewController *vc = [UserProfileViewController new];
    vc.uid = uid;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma EventCell Delegate
- (void)eventCellDidClickedAvatar:(id)eventCell{
    Event *evt = [eventCell event];
    [self openUserProfileViewController:evt.operator.uid];
    DLOG(@"event:%@",evt);
}
- (void)eventCellDidClickedNickName:(id)eventCell{
    Event *evt = [eventCell event];
    [self openUserProfileViewController:evt.operator.uid];
    DLOG(@"event:%@",evt);
}
- (void)eventCellDidClickedContent:(id)eventCell{
    Event *evt = [eventCell event];
    DLOG(@"event:%@",evt);
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
